package com.softserve.academy.food.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.softserve.academy.food.dao.DishDao;
import com.softserve.academy.food.dao.OrderDao;
import com.softserve.academy.food.dao.UserDao;
import com.softserve.academy.food.entity.OrderInfo;
import com.softserve.academy.food.model.ContentModel;
import com.softserve.academy.food.model.OrderModel;
import com.softserve.academy.food.model.UserModel;

@Service("orderService")
public class OrderService
{
	@Autowired
	private OrderDao	orderDao;
	
	@Autowired
	private DishDao dishDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private SessionFactory session;

	@Transactional
	public ArrayList<OrderModel> getAll()
	{
		ArrayList<OrderModel> models = new ArrayList<OrderModel>();
		for (OrderInfo m : orderDao.getAll())
		{
			models.add(m.toModel());
		}
		return models;
	}

	@Transactional
	public OrderModel get(int id)
	{
		return orderDao.get(id).toModel();
	}

	// working 
	//  but i dont realy understand what Status 4 ?  and where we will set it.
	@Transactional
	public void add(Integer user_id, HashMap<Integer,Integer> spec)
	{
		List<ContentModel> list = new ArrayList<ContentModel>();
		UserModel user = userDao.get(user_id).toModel();
		Set<Integer> set = spec.keySet();
		for(int id : set)
		{
			ContentModel cm = new ContentModel();
			cm.setDish(dishDao.get(id).toModel());
			cm.setQuantity(spec.get(id));
			list.add(cm);
		}
		orderDao.add((new OrderModel(0,user,new Date(),'A',list)).toEntity());
	}

	@Transactional
	public void delete(int id)
	{
		orderDao.delete(id);
	}

}
